package top.lyc829.zzl_android.ui.team.add_team;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import java.io.IOException;
import java.util.List;

import okhttp3.Response;
import top.lyc829.zzl_android.logic.bean.BaseBean;
import top.lyc829.zzl_android.logic.bean.ResultBean;
import top.lyc829.zzl_android.logic.network.Network;
import top.lyc829.zzl_android.logic.util.HttpUtil;
import top.lyc829.zzl_android.logic.util.ReplayType;
import top.lyc829.zzl_android.logic.util.Utils;
import top.lyc829.zzl_android.logic.util.gson.JsonUtilNew;

/**
 * @Author lyc
 * @create 2021-03-27 15:32
 */
public class AddTeamViewModel extends ViewModel {

    private MutableLiveData<List<BaseBean>> baseListLiveData;

    public MutableLiveData<List<BaseBean>> getBaseListLiveData() {
        if (baseListLiveData == null) {
            baseListLiveData = new MutableLiveData<>();
        }
        return baseListLiveData;
    }


    public void getBasesInCreateTeam(String name, int page) {

        Network.getSearchDetail(ReplayType.BASE, name, page, Utils.getUserId(), new HttpUtil.MyCallback() {
            @Override
            public void success(Response res) throws IOException {
                ResultBean<List<BaseBean>> resultBean = JsonUtilNew
                        .fromJsonArray(res.body().string(), BaseBean.class);

                if (resultBean.getCode().equals(200)) {
                    getBaseListLiveData().postValue(resultBean.getData());
                } else {
                    failed(new IOException(resultBean.getMsg()));
                }
            }

            @Override
            public void failed(IOException e) {
                Utils.Sout(e.getMessage());
            }
        });
    }

}
